var is_debug=true;
var code = getParameter("code");//微信公众号授权之后的code

if(window.location.href.indexOf("pocket.all-star.top")>=0){
    is_debug=false;
}
else {
    is_debug=true;
}

function User(openid,mobile,nickname,avatar,uid,role,referee,parent_code,redpacket_money=0,redpacket_num=0,isgetredpacket=0) {
    this.openid=openid;
    this.mobile=mobile;
    this.nickname=nickname;
    this.avatar=avatar;
    this.uid=uid;
    this.role=role;
    this.referee=referee;
    this.parent_code=parent_code;
    this.redpacket_money = redpacket_money;
    this.redpacket_num = redpacket_num;
    this.isgetredpacket = isgetredpacket;
    
}

User.prototype.isLogin=function () {
    if(this.openid != undefined && this.openid != "undefined" && this.openid != null && this.openid.length >0&&this.uid != undefined&& this.uid != "undefined" && this.uid != null && this.uid.length >0){
        return true;
    }
    return false;
};

User.prototype.partnerGrade=function () {
    if(this.parent_code != undefined && this.parent_code != null && this.parent_code.length >0){
        var arr=this.parent_code.split('_');
        return arr.length-1;
    }
    return 0;
};

User.prototype.cacheUserInfo=function(openid,nickname,mobile,avatar,uid,role,referee,parent_code,redpacket_money=0,redpacket_num=0,isgetredpacket=0){
    if(window.localStorage){
        localStorage.setItem("uid",openid);
        localStorage.setItem("openid",openid);
        localStorage.setItem("nickname",nickname);
        localStorage.setItem("mobile",mobile);
        localStorage.setItem("avatar",avatar);
        localStorage.setItem("uid",uid);
        localStorage.setItem("role",role);//用户角色 1：普通用户 2：合伙人
        localStorage.setItem("referee",referee);//推荐人
        localStorage.setItem("parent_code",parent_code);//级别编码
		
		localStorage.setItem("redpacket_money",redpacket_money);
        localStorage.setItem("redpacket_num",redpacket_num);
        localStorage.setItem("isgetredpacket",isgetredpacket);

        this.openid=localStorage.getItem("openid");
        this.nickname=localStorage.getItem("nickname");
        this.mobile=localStorage.getItem("mobile");
        this.avatar=localStorage.getItem("avatar");
        this.uid=localStorage.getItem("uid");
        
        this.redpacket_money = redpacket_money;
    	this.redpacket_num = redpacket_num;
    	this.isgetredpacket = isgetredpacket;
        
    }
    return this;
}


User.prototype.getUserInfoFromCache=function(){
    if(window.localStorage){
        this.openid=localStorage.getItem("openid");
        this.nickname=localStorage.getItem("nickname");
        this.mobile=localStorage.getItem("mobile");
        this.avatar=localStorage.getItem("avatar");
        this.uid=localStorage.getItem("uid");
        this.role=localStorage.getItem("role");
        this.referee=localStorage.getItem("referee");
        this.parent_code=localStorage.getItem("parent_code");
        
        this.redpacket_money = localStorage.getItem("redpacket_money");
    	this.redpacket_num = localStorage.getItem("redpacket_num");
    	this.isgetredpacket = localStorage.getItem("isgetredpacket");
    }
    return this;
}

User.prototype.clearUserInfoCache=function(){
    if(window.localStorage){
        this.openid="";
        this.nickname="";
        this.mobile="";
        this.avatar="";
        this.uid="";
        this.role="";
        this.referee="";
        this.parent_code="";
        this.redpacket_money = 0;
    	this.redpacket_num = 0;
    	this.isgetredpacket = 0;
        localStorage.clear();
    }
}

var getUserEntityFromCache=function(){
    var user=new User("","","","",0,0,0,'');
    if(window.localStorage){
        if(is_debug){
            // user=new User("oAnxN0SVrDe9OxuKRSmmHSpxg92s","18721886115","Vivi","http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIlCa0fwksPMXsRBvgzHQ7szrNKE9A4mZjm9v8cJDsTQDedib0xsMlzgkrGeORq0Q8IGsIdspJ9m1Q/0","11",'1','','');
//          user=new User("oAnxN0SVrDe9OxuKRSmmHSpxg92s","18721886115","Vivi","http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIlCa0fwksPMXsRBvgzHQ7szrNKE9A4mZjm9v8cJDsTQDedib0xsMlzgkrGeORq0Q8IGsIdspJ9m1Q/132","11",'0','0','0');
//			user=new User("oAnxN0SVrDe9OxuKRSmmHSpxg92s","18721886115","Vivi","http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIlCa0fwksPMXsRBvgzHQ7szrNKE9A4mZjm9v8cJDsTQDedib0xsMlzgkrGeORq0Q8IGsIdspJ9m1Q/132","11",'0','0','0',0,0,0);
			user=new User("oAnxN0SVrDe9OxuKRSmmHSpxg92s","","Vivi","http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIlCa0fwksPMXsRBvgzHQ7szrNKE9A4mZjm9v8cJDsTQDedib0xsMlzgkrGeORq0Q8IGsIdspJ9m1Q/132","11",'0','0','0',0,0,0);

        }
        else {
            user=new User(localStorage.getItem("openid"),localStorage.getItem("mobile"),localStorage.getItem("nickname"),localStorage.getItem("avatar"),localStorage.getItem("uid"),localStorage.getItem("role"),localStorage.getItem("referee"),localStorage.getItem("parent_code"));
        }
    }
    return user;
}

//通过接口从微信获取用户信息
function getUserInfo(callback){
    $.ajax({
        type: "get",
        async: true,
        url: "/v1/wx-user/get-wx-user-info",
        data: {code:code},
        dataType: "json",
        beforeSend:function(){

        },
        success: function(data){

            if(data.code=="success"){

                if(callback){
                    callback(data.data);
                }
            }
            else {
                if(callback){
                    callback(data.message);
                }
            }

        },
        error: function(){
            showAlert("获取用户授权信息失败");
        }
    });
};

//通过本站用户信息
function getUserInfoOfApp(uid,callback){
    $.ajax({
        type: "get",
        async: true,
        url: "/v1/wx-user/get-user-info",
        data: {uid:uid},
        dataType: "json",
        beforeSend:function(){
        },
        success: function(data){

            if(data.code=="success"){
                if(callback){
                    callback(data.data);
                }
            }
            else {
                if(callback){
                    callback(data.message);
                }
            }

        },
        error: function(){

            if(callback){
                callback(null);
            }
        }
    });
};




//请求微信用户授权
function getUserAuthorize(){
    window.location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri="+encodeURIComponent(window.location.href)+"&response_type=code&scope=snsapi_userinfo&state="+state+"&#wechat_redirect";
}
